<?php
  // 新增分类后端保存程序
  include_once('../conn.php');
  $category_name = $_POST['category_name'];
  $category_explain = $_POST['category_explain'];
  
  $sql = "select * from category where category_name = '$category_name'";
 
  $result = mysqli_query($conn, $sql);
  if(mysqli_num_rows($result) > 0) {
    alert('该分类已存在', 'category_new.php');
    exit();
  }
  
  if(strlen($category_name) < 2) {
    alert('分类名称不能小于2位', 'category_new.php');
    exit();
  }

  // 新增一条分类信息
  $sql = "insert into category (category_name, category_explain) values ('$category_name', '$category_explain')";
  $result = mysqli_query($conn, $sql);
  if($result) {
    alert('添加成功', 'category_list.php');
  } else {
    alert('添加失败', 'category_save.php');
  }

  mysqli_close($conn);
?>